package com.siemens.industrial.app;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by David on 2016/11/16.
 */
public class SocketJob implements Job {
    private static final int SOCKET_PORT = 10001;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("Hello Job for Socket");
        ServerSocket serverSocket = null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(SOCKET_PORT);
            System.out.println("The Server is start: " + serverSocket);
            while(true) {
                socket = serverSocket.accept();

                System.out.println("socket:"+socket);

                new SocketHandler(socket);
            }
        } catch (IOException e) {
            try {
                assert socket != null;
                socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                assert serverSocket != null;
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
